package net.eascn.babyClone.view.comp
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.utils.getQualifiedClassName;
	
	/**
	 * 
	 * @author eas
	 * <
	 * <frame index=''>
	 * <comp type='image' name='img_1' x='' y='' z='' width='' height='' rotate=''/>
	 * </frame>
	 */
	public class SwfFrameAnalyse extends EventDispatcher
	{
		public function SwfFrameAnalyse()
		{
		}
		
		public function set source(value:String):void
		{
			var req:URLRequest = new URLRequest(value );
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
			loader.load(req);
		}
		
		private var loader:Loader= new Loader();
		
		private var _albumXML:XML = new XML();
		public function get albumXml():XML
		{
			return _albumXML;
		}
		
		
		private function onLoadComplete(event:Event):void
		{
			var content:DisplayObject = loader.content;
			var xml:XML = <album />;
			if(content is MovieClip)
			{
				var mc:MovieClip = content as MovieClip;
				xml.@frameCount = mc.totalFrames;
				trace('total frame',mc.totalFrames);
				var  totalFrames:int = mc.totalFrames; 
				for(var i:int = 1; i <= totalFrames ; i ++)
				{
					mc.gotoAndStop(i);
					trace('numChilren in this frame',mc.numChildren);
					var frameXml:XML = <frame/>;
					frameXml.@index = i;
					var numChildren:int = mc.numChildren;
					for(var j:int=0 ; j < numChildren; j++)
					{
						var child:DisplayObject  =  mc.getChildAt(j);
						var className:String = getQualifiedClassName(child);
						
						var childXml :XML = <comp />;
						childXml.@type = className ; 
						childXml.@name = child.name;
						childXml.@x = child.x;
						childXml.@y = child.y;
						childXml.@z = j;
						childXml.@width = child.width;
						childXml.@height = child.height;
						childXml.@rotation = child.rotation;
						
						frameXml.appendChild(childXml);
					}
					xml.appendChild(frameXml);
				}
			}else if(content is Sprite)
			{
				
			}
			_albumXML = xml.copy();
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}